define(['zepto', 'butterfly/view','../../bsl',
    'text!./orientation_compass.html'
], function($, View, bsl, orientation_compass) {
    var me = null;

    var view = View.extend({
        events: {

            // ！！！！ 由于 引入 touch.js 后，tap事件会变成双击，所以 本页面内不能使用tap，用click代替。
            "tap .openDoc" : "openDoc",
            "tap #back" : "back",

      
        },
        render: function() {
            me = this;
        },

        onShow: function() {

            // 绑定自定义事件 滑动切换日历等           
            this.bslReady();

        },
            bslReady:function (){
            // 锁定只能竖屏显示
            bsl.screen.lockOrientation( "portrait-primary" );
        },
        // if(window.bsl){
        //     bslReady();
        // }else{
        //     document.addEventListener("bslready",bslReady,false);
        // }
        // 解锁并关闭
         // _back:window.back,
        unlockback:function (){
            bsl.screen.unlockOrientation();
        },
        // window.back=unlockback;

        var t, x;
        var ld;
        var tran="0.1s", empty="0s";
        var bani=false, bsp=true;
        update:function (d) {
            var c = ld - d;
            var ac = Math.abs(c);
            if (ac <= 1) {
                return;
            } else if (ac > 180) {
                if ( bani ) {
                    t.style.webkitTransitionDuration = empty;
                }
                ld = (c > 0) ? ld - 360 : ld + 360;
                t.style.webkitTransform = "rotate(" + (bsp?(-ld):(ld)) + "deg)";
                setTimeout(adjust, 1);
            } else {
                t.style.webkitTransform = "rotate(" + (bsp?(-d):(d)) + "deg)";
            }
            ld = d;
            var r = Math.round(d);
            var txt = "";
            if (r >= 338 || r < 23) {
                txt = "北";
            } else if (r < 68) {
                txt = "东北";
            } else if (r < 113) {
                txt = "东";
            } else if (r < 158) {
                txt = "东南";
            } else if (r < 203) {
                txt = "南";
            } else if (r < 248) {
                txt = "西南";
            } else if (r < 293) {
                txt = "西";
            } else {
                txt = "西北";
            }
            x.innerHTML = txt + r + "°";
        },
        adjust:function () {
            if ( bani ) {
                t.style.webkitTransitionDuration = tran;
            }
            t.style.webkitTransform = "rotate(" + (bsp?(-ld):(ld)) + "deg)";
        },
        gohome:function () {
            bsl.webview.currentWebview().close();
        },
        document.addEventListener("bslready", function() {
            t = document.getElementById("compass");
            x = document.getElementById("inf");

            if ( bsl.os.name == "iOS" ) {
                t.style.webkitTransition = "all 0.1s linear";
                bani = true;
                if ( bsl.device.model.indexOf("iPhone") < 0 ) {
                    bsp = false;
                    document.getElementById("tip").innerText = "此设备方向数据不准确";
                }
            }
            bsl.orientation.watchOrientation(function(r) {
                update(bsp?r.trueHeading:r.alpha);//update(r.alpha);
            }, function(e) {
                // error
            }, {
                frequency : 100
            });
        }, false);
        // Adjective select.
        document.onselectstart = function() {
            return false;
        },
        back : function(e){
             this.unlockback();
             window.history.go(-1);
        },
      
            });
    return view;
});
